use openmls::framing::ProtocolMessage;

/// Batch of inbound messages that we process all at once to avoid excessive disk IO.
#[derive(Clone)]
pub struct MessageBatch {
    protocol_messages: Vec<ProtocolMessage>,
}

impl MessageBatch {
    pub fn new(protocol_messages: Vec<ProtocolMessage>) -> Self {
        if protocol_messages.is_empty() {
            panic!("message_batch: must be nonempty list of messages");
        }

        let pm0 = &protocol_messages[0];
        if !protocol_messages[1..]
            .iter()
            .all(|pm| pm.group_id() == pm0.group_id())
        {
            panic!("message_batch: not all messages for same group");
        }

        Self { protocol_messages }
    }

    pub fn into_inner(self) -> Vec<ProtocolMessage> {
        self.protocol_messages
    }
}
